package com.ninjas.module.error.controller;

import com.ninjas.module.common.page.PageResult;
import com.ninjas.module.common.page.PageUtils;
import com.ninjas.module.error.domain.dto.ErrorStatisticsDTO;
import com.ninjas.module.error.domain.model.Error;
import com.ninjas.module.error.domain.model.UserOptLog;
import com.ninjas.module.error.domain.vo.IndexErrorListVO;
import com.ninjas.module.error.domain.vo.IndexErrorStatisticsVO;
import com.ninjas.module.error.domain.vo.IndexUserOptLogListVO;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

public class IndexConverter {

    public static PageResult toIndexErrorListVO(List<Error> list) {
        if (CollectionUtils.isEmpty(list)) {
            return PageResult.empty();
        }
        List<IndexErrorListVO> VOList = list.stream().map(v -> {
            IndexErrorListVO VO = new IndexErrorListVO();
            VO.setContent(v.getContent());
            VO.setCreateTime(v.getCreateTime());
            return VO;
        }).collect(Collectors.toList());
        return PageResult.success(PageUtils.getTotal(list), VOList);
    }

    public static PageResult toIndexUserOptLogListVO(List<UserOptLog> list) {
        if (CollectionUtils.isEmpty(list)) {
            return PageResult.empty();
        }
        List<IndexUserOptLogListVO> VOList = list.stream().map(v -> {
            IndexUserOptLogListVO VO = new IndexUserOptLogListVO();
            VO.setUserName(v.getOptUserName());
            VO.setContent(v.getContent());
            VO.setCreateTime(v.getCreateTime());
            return VO;
        }).collect(Collectors.toList());
        return PageResult.success(PageUtils.getTotal(list), VOList);
    }

    public static IndexErrorStatisticsVO toIndexErrorStatisticsVO(
            ErrorStatisticsDTO all,
            ErrorStatisticsDTO year,
            ErrorStatisticsDTO month,
            ErrorStatisticsDTO day) {
        IndexErrorStatisticsVO VO = new IndexErrorStatisticsVO();
        if (all != null) {
            VO.setCount(all.getCount());
            VO.setErrorCount(all.getErrorCount());
            VO.setIgnoreCount(all.getIgnoreCount());
        }
        if (year != null) {
            VO.setYearCount(year.getCount());
            VO.setYearErrorCount(year.getErrorCount());
            VO.setYearIgnoreCount(year.getIgnoreCount());
        }
        if (month != null) {
            VO.setMonthCount(month.getCount());
            VO.setMonthErrorCount(month.getErrorCount());
            VO.setMonthIgnoreCount(month.getIgnoreCount());
        }
        if (day != null) {
            VO.setDayCount(day.getCount());
            VO.setDayErrorCount(day.getErrorCount());
            VO.setDayIgnoreCount(day.getIgnoreCount());
        }
        return VO;
    }
}
